﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Context;
using OFB.Domain;
using System.Data;

namespace OFB.BLL.Repository
{
    public class CourseRepositoryBLL : ICourseRepositoryBLL
    {
        public CourseContext context;

        public CourseRepositoryBLL()
        {
            context = new CourseContext();
        }

        public ICollection<Course> FindAll()
        {
            return context.Course.Init("Course_ListAll")
                               .MapCollection<Course>()
                               .GetCollection();
        }

        public Course Find(int courseId)
        {
            return context.Course.Init("Course_ListOne", context.Course.BuildParameter("CourseId", System.Data.DbType.Int32, courseId))
                               .Map()
                               .Get();
        }

        public Course Find(string courseName)
        {
            return context.Course.Init("Course_CheckExistCourse", context.Course.BuildParameter("CourseName", System.Data.DbType.Int32, courseName))
                                 .Map()
                                 .Get();
                               
        }

        public Course Find(int courseId, string courseName)
        {
            return context.Course.Init("Course_CheckCourseName", context.Course.BuildParameter("CourseId", System.Data.DbType.Int32, courseId),
                                                                 context.Course.BuildParameter("CourseName", System.Data.DbType.String, courseName))
                               .Map()
                               .Get();
        }

        public Course CheckFbTemplate(int fbTemplateId)
        {
            return context.Course.Init("Course_IsDisabledTemplate", context.Course.BuildParameter("FbTemplateId", System.Data.DbType.Int32, fbTemplateId))
                               .Map()
                               .Get();
        }

        public Course CheckTraineeInCourse(int courseId)
        {
            return context.Course.Init("Course_CheckTraineeInCourse", context.Course.BuildParameter("CourseId", System.Data.DbType.Int32, courseId))
                              .Map()
                              .Get();
        }

        public void GetTraineeCount(string courseId, out int traineeCount, out int subjectCount)
        {
            IDataParameter tcount = context.Course.BuildParameter("TraineeCount", DbType.Int32, ParameterDirection.Output);
            IDataParameter scount = context.Course.BuildParameter("SubjectCount", DbType.Int32, ParameterDirection.Output);

            context.Course.Init("Course_GetTraineeSubject", context.Course.BuildParameter("CourseId", System.Data.DbType.Int32, courseId), tcount, scount);

            traineeCount = (int)tcount.Value;
            subjectCount = (int)scount.Value;
        }

        public ICollection<Course> FindCourse(string courseName, DateTime fromDate, DateTime toDate, string status, int offset, int maxitem, out int count)
        {
            IDataParameter countParam = context.Course.BuildParameter("Count", DbType.Int32, ParameterDirection.Output);

            ICollection<Course> courselist= context.Course.Init("Course_Search2", context.Course.BuildParameter("CourseName", DbType.String, courseName),
                                                        context.Course.BuildParameter("FromDate", DbType.DateTime, fromDate),                        
                                                        context.Course.BuildParameter("ToDate", DbType.DateTime, toDate),
                                                        context.Course.BuildParameter("Status", DbType.Boolean, status),
                                                        context.Course.BuildParameter("Offset", DbType.Int32, offset),
                                                            context.Course.BuildParameter("Maxitem", DbType.Int32, maxitem),
                                                            countParam)
                                        .MapCollection<Course>()
                                        .GetCollection();
            count = (int)countParam.Value;
            return courselist;
        }

        
       

        public int CreateCourse(string courseName, DateTime startDate, DateTime finishDate, int fbTemplateId)
        {
            IDataParameter courseIdParam = context.Course.BuildParameter("CourseId", DbType.Int32, ParameterDirection.Output);
            context.Course.Execute("Course_Create", context.Course.BuildParameter("CourseName", DbType.String, courseName),
                                            context.Course.BuildParameter("StartDate", DbType.String, startDate),
                                            context.Course.BuildParameter("FinishDate", DbType.String, finishDate),
                                            context.Course.BuildParameter("FBTemplateId", DbType.Int32, fbTemplateId),
                                            courseIdParam);

            return (int)courseIdParam.Value;
        }

        public int UpdateCourse(int courseId, string courseName, DateTime startDate, DateTime finishDate)
        {
            IDataParameter rowEffectedParam = context.Course.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);
            context.Course.Execute("Course_Update", context.Course.BuildParameter("CourseId", DbType.String, courseId),
                                               context.Course.BuildParameter("CourseName", DbType.String, courseName),
                                               context.Course.BuildParameter("StartDate", DbType.String, startDate),
                                               context.Course.BuildParameter("FinishDate", DbType.String, finishDate),
                                               rowEffectedParam);

            return (int)rowEffectedParam.Value;
        }

        public void DisableCourse(int courseId)
        {

            context.Course.Execute("Course_Disable", context.Course.BuildParameter("CourseId", DbType.Int32, courseId));

            
        }

        public void EnableCourse(int courseId)
        {

            context.Course.Execute("Course_Enable", context.Course.BuildParameter("CourseId", DbType.Int32, courseId));


        }

        public ICollection<FeedbackTemplate> GetFeedTemp()
        {
            return context.FeedbackTemplate.Init("Course_GetFbTemplate")
                                        .MapCollection<FeedbackTemplate>()
                                        .GetCollection();
        }


      
    }
}
